package com.lanou.sfu.songforu.music_bd;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lanou.sfu.songforu.R;



import java.util.ArrayList;
import java.util.List;

public class MusicActivityFragment extends Fragment implements  OnItemClickListener, View.OnClickListener {
    /**
     * Called when the activity is first created.
     */

    private ListView mListView;
    private ManaListAdapter mManaListAdapter; // 定义适配器

    private ImageView music_back;
    private List<ManaMusic> list = new ArrayList<ManaMusic>();



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 绑定布局
        View view = inflater.inflate(R.layout.aidl_music_main,null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 初始化
        mListView = (ListView) getActivity().findViewById(R.id.music_pager);
        music_back = (ImageView) getActivity().findViewById(R.id.music_back);

        // 设置监听
        mListView.setOnItemClickListener(this);
        mListView.setCacheColorHint(0); // 去除listView拖动背景颜色
        music_back.setOnClickListener(this);

        // 绑定适配器
        mManaListAdapter = new ManaListAdapter(getActivity(), readAllMusicByDB());
        mListView.setAdapter(mManaListAdapter);
    }



    /**
     * 从数据库中读取歌曲的信息
     *
     * @return
     */
    public List<ManaMusic> readAllMusicByDB() {

        Cursor cursor = getActivity().getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                null);
        if (cursor.getCount() == 0) {
            return list;
        }
        cursor.moveToFirst();
        do {
            ManaMusic music = new ManaMusic();
            music.mMusicArtist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            music.mMusicName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            music.mMusicTime = getTime(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));
            music.mMusicPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            list.add(music);

            Log.d("MusicActivityFragment", music.mMusicName);
        } while (cursor.moveToNext());
        cursor.close();
        Log.d("MusicActivityFragment", "list:" + list);

        return list;

    }

    /**
     * 将时间的毫秒转换成为00:00格式
     *
     * @param time 时间
     * @return 00:00格式
     */
    public String getTime(int time) {
        time /= 1000;
        int minute = time / 60;
        int second = time % 60;
        minute %= 60;
        return String.format("%02d:%02d", minute, second);
    }


    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }



}